﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;


public class XL_BANG : DataTable
{
    //Khai bao bien thanh phan
    SqlDataAdapter bo_doc_ghi = null;
    string chuoi_SQL = null;
    string ten_bang = null;
    static SqlConnection ket_noi = null;
    //Khai bao thuoc tinh
    public string Chuoi_SQL
    {
        get { return chuoi_SQL; }
        set { chuoi_SQL = value; }
    }
    public string Ten_bang
    {
        get { return ten_bang; }
        set { ten_bang = value; }
    }
    public static SqlConnection Ket_noi
    {
        get { return ket_noi; }
        set { ket_noi = value; }
    }
    public int So_dong
    {
        get { return this.DefaultView.Count; }
    }
    //Khai bao ham khoi tao
    public XL_BANG()
        : base()
    {
    }
    public XL_BANG(string pTen_bang)
        : base(pTen_bang)
    {
        Ten_bang = pTen_bang;
        Doc_bang();
    }
    public XL_BANG(string pTen_bang, string pChuoi_SQL)
        : base(pTen_bang)
    {
        Ten_bang = pTen_bang;
        Chuoi_SQL = pChuoi_SQL;
        Doc_bang();
    }
    //Khai bao ham xu ly
    private void Doc_bang()
    {
        if (Chuoi_SQL == null) Chuoi_SQL = "Select * From " + Ten_bang;
        Tao_ket_noi();
        try
        {
            bo_doc_ghi = new SqlDataAdapter(Chuoi_SQL, Ket_noi);
            bo_doc_ghi.FillSchema(this, SchemaType.Mapped);
            bo_doc_ghi.Fill(this);
            bo_doc_ghi.SelectCommand.CommandText = "Select * From " + Ten_bang;
            SqlCommandBuilder bo_phat_sinh = new SqlCommandBuilder(bo_doc_ghi);
            bo_doc_ghi.RowUpdated += new SqlRowUpdatedEventHandler(bo_doc_ghi_RowUpdated);
        }
        catch (Exception ex)
        {
            throw new Exception(ex.ToString());
        }
    }

    private void bo_doc_ghi_RowUpdated(object sender, SqlRowUpdatedEventArgs e)
    {
        if (this.PrimaryKey[0].AutoIncrement)
        {
            this.PrimaryKey[0].ReadOnly = false;
            if (e.Status == UpdateStatus.Continue && e.StatementType == StatementType.Insert)
            {
                SqlCommand bo_lenh = new SqlCommand("Select @@IDENTITY", Ket_noi);
                e.Row[0] = bo_lenh.ExecuteScalar();
                e.Row.AcceptChanges();
            }
        }
    }
    private void Tao_ket_noi()
    {
        if (Ket_noi == null) Ket_noi = new SqlConnection(PHAN_MEM.Chuoi_ket_noi);
    }
    public bool Ghi()
    {
        bool ket_qua = true;
        try
        {
            bo_doc_ghi.Update(this);
            this.AcceptChanges();
        }
        catch
        {
            this.RejectChanges();
            ket_qua = false;
        }
        return ket_qua;
    }
    public void Khong_ghi()
    {
        this.RejectChanges();
    }
    protected bool Kiem_tra_vi_tri(int i)
    {
        return (i >= 0 && i < this.Rows.Count);
    }
}

